package com.ruoyi.eam.service.impl;

import java.util.List;

import com.ruoyi.eam.doman.EamAsset;
import com.ruoyi.eam.doman.EamStock;
import com.ruoyi.eam.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.eam.doman.EamStockD;
import com.ruoyi.eam.service.IEamStockDService;

import javax.annotation.Resource;

/**
 * 入库从Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-06-20
 */
@Service
public class EamStockDServiceImpl implements IEamStockDService 
{
    @Resource
    private EamStockDMapper eamStockDMapper;

    @Resource
    private EamTypeMapper eamTypeMapper;

    @Resource
    private EamStockMapper eamStockMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private EamAssetMapper eamAssetMapper;



    

    /**
     * 查询入库从
     * 
     * @param stokId 入库从主键
     * @return 入库从
     */
    @Override
    public EamStockD selectEamStockDByStokId(Long stokId)
    {
        return eamStockDMapper.selectEamStockDByStokId(stokId);
    }

    /**
     * 查询入库从列表
     * 
     * @param eamStockD 入库从
     * @return 入库从
     */
    @Override
    public List<EamStockD> selectEamStockDList(EamStockD eamStockD)
    {
        return eamStockDMapper.selectEamStockDList(eamStockD);
    }

    /**
     * 新增入库从
     * 
     * @param eamStockD 入库从
     * @return 结果
     */
    @Override
    public int insertEamStockD(List<EamAsset> assets)
    {
        //获取最后一条变更id
        //Long chgIdLast = chgHMapper.selectEamChgHList(null).get(chgHMapper.selectEamChgHList(null).size() - 1).getAlterationId();
        Long stockDIdLast = eamStockMapper.selectEamStockList(null).get(eamStockMapper.selectEamStockList(null).size() - 1).getStockId();
        System.out.println(stockDIdLast);
        for (EamAsset asset : assets) {
            EamStockD eamStockD = new EamStockD();
            eamStockD.setStokId(stockDIdLast);
            eamStockD.setAssetId(asset.getAssetId());
            eamStockD.setCount(asset.getAssetNum());
            //添加变更从表信息
            int num = eamStockDMapper.insertEamStockD(eamStockD);
            //修改资产信息
            int num1 = eamAssetMapper.updateEamAsset(asset);
            if(num == 0 || num1 == 0){
                return 0;
            }
        }
        return 1;
    }

    /**
     * 修改入库从
     * 
     * @param eamStockD 入库从
     * @return 结果
     */
    @Override
    public int updateEamStockD(EamStockD eamStockD)
    {
        return eamStockDMapper.updateEamStockD(eamStockD);
    }

    /**
     * 批量删除入库从
     * 
     * @param stokIds 需要删除的入库从主键
     * @return 结果
     */
    @Override
    public int deleteEamStockDByStokIds(Long[] stokIds)
    {
        return eamStockDMapper.deleteEamStockDByStokIds(stokIds);
    }

    /**
     * 删除入库从信息
     * 
     * @param stokId 入库从主键
     * @return 结果
     */
    @Override
    public int deleteEamStockDByStokId(Long stokId)
    {
        return eamStockDMapper.deleteEamStockDByStokId(stokId);
    }
}
